<template>
  <div class="container">
    <ul>
      <el-card shadow="always">
        <li>
          <div class="title">
            <span class="line"></span>
            <span class="title-text">系统设置</span>
            <span class="line"></span>
          </div>
          <div class="content">
            <div>
              <span>是否允许一个账户同时在后台的不同地点登录:</span>  <el-switch v-model="from.allowLoginInTheSameTime" />
            </div>
            <div>
              <span>是否允许一个账户同时在多个前台登录:</span>  <el-switch @change="changeLoginFronten" v-model="from.allowLoginInTheSameTimeOfFrontend" />
            </div>
            <div>
              <span>是否允许一个账户同时在相同的前台类型登录:</span>  <el-switch @change="changeAllowLoginInTheSameTimeOfTheSameFrontend" v-model="from.allowLoginInTheSameTimeOfTheSameFrontend" />
            </div>
          </div>
        </li>
        <div class="bottom">
          <el-button type="primary" @click="changeSysConfig">修改</el-button>
        </div>
      </el-card>
    </ul>
  </div>
</template>

<script lang='ts' setup>
import {
  defineExpose, reactive, getCurrentInstance, ref, onMounted,
} from 'vue';
import Request from '@/api/request/request';
import { ElMessage } from 'element-plus';

const { proxy } = getCurrentInstance();
const from = ref({
  id: 0,
  allowLoginInTheSameTime: false,
  allowLoginInTheSameTimeOfFrontend: false,
  allowLoginInTheSameTimeOfTheSameFrontend: false,
});

const changeLoginFronten = (val:boolean) => {
  from.value.allowLoginInTheSameTimeOfFrontend = val;
  from.value.allowLoginInTheSameTimeOfTheSameFrontend = !val;
};

const changeAllowLoginInTheSameTimeOfTheSameFrontend = (val:boolean) => {
  from.value.allowLoginInTheSameTimeOfTheSameFrontend = val;
  from.value.allowLoginInTheSameTimeOfFrontend = !val;
};

const getConfig = async () => {
  const resData:any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_AUTHORITY_CONFIG}/all`).get();
  if (resData.code === 200) {
    console.log(resData.data);
    from.value.id = resData.data[0].id;
    from.value.allowLoginInTheSameTime = resData.data[0].allowLoginInTheSameTime;
    from.value.allowLoginInTheSameTimeOfFrontend = resData.data[0].allowLoginInTheSameTimeOfFrontend;
    from.value.allowLoginInTheSameTimeOfTheSameFrontend = resData.data[0].allowLoginInTheSameTimeOfTheSameFrontend;
  }
};
onMounted(() => {
  getConfig();
});

const changeSysConfig = async () => {
  const resData:any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_AUTHORITY_CONFIG}`, from.value).put();
  if (resData.code === 200) {
    ElMessage.success('修改成功');
  }
};
</script>

<style lang="scss" scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.container {
  ul {
    list-style: none;
    li {
      width: 100%;
      height: auto;
      display: flex;
      flex-direction: column;
      .title {
        display: flex;
        justify-content: center;
        align-items: center;
        text-align: center;
        span:nth-child(1) {
          flex: 1;
        }
        span:nth-child(3) {
          flex: 1;
        }
        .line {
          display: inline-block;
          width: 50%;
          max-width: 50%;
          border-bottom: 1px solid rgb(214,222,228);
        }
      }
      .content {
        width: 100%;
        padding: 1% 10%;
        display: flex;
        flex-direction: column;
        div {
          display: flex;
          align-items: center;
          margin-bottom: 10px;
          span {
            display: inline-block;
            height: 32px;
            margin-right: 20px;
          }
        }
      }
    }
  }
  .bottom {
    width: 100%;
    display: flex;
    justify-content: center;
    .el-button {
      width: 50%;
      text-align: center;
    }
  }
}
</style>
